package CarRental;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws SQLException {
        //在主函数里面调用这个ConnectionString()方法
        Connection conn = ConnectionString();
        if (conn != null) {
            //只要连接信息不为空
            System.out.println("数据库连接成功！");
        }
        else {
            System.out.println("数据库连接失败！");
        }
        conn.close();
    }

    //返回数据库连接信息
    public static Connection ConnectionString() {
        Connection conn = null;
        try {
            // 读取jdbc.properties文件
            InputStream is = Main.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties props = new Properties();
            props.load(is);
            // 获取数据库连接
            Class.forName(props.getProperty("jdbc.driverClassName"));
            conn = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.username"), props.getProperty("jdbc.password"));
        }
        catch (IOException |
               ClassNotFoundException |
               SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

